package com.kotlin.android.platoon.app

import android.os.Handler
import android.os.Looper
import com.kotlin.android.app.R
import com.kotlin.android.app.databinding.ActSplashBinding
import com.kotlin.android.app.handFlashAD
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.core.BaseViewModel
import com.kotlin.android.ktx.ext.dimension.screenHeight
import com.kotlin.android.ktx.ext.dimension.screenWidth
import com.kotlin.android.ktx.ext.immersive.immersive
import com.kotlin.android.ktx.ext.log.e
import com.kotlin.android.ktx.lifecycle.KtxActivityManager
import com.kotlin.android.media.image.coil.loadImage
import com.kotlin.android.widget.dialog.PrivacyDialog

/**
 * 启动页，简单逻辑：
 * 1，隐私协议 - 协议弹窗；
 * 2，启动SDK初始化；
 * 3，闪屏广告逻辑判断 - 闪屏广告页；
 * 4，进入主页。
 *
 * 注意：延时关闭启动页，确保交互的稳定性。
 */
class MainActivity : BaseVMActivity<BaseViewModel, ActSplashBinding>() {
    //2024-04-01 00:01:25
    private val entTime = 1711900885000

    private var dialog: PrivacyDialog? = null
    override fun initTheme() {
        super.initTheme()
        immersive()
            .transparentStatusBar(isFitsSystemWindows = false)
            .statusBarDarkFont(true)
    }

    override fun initView() {
        val currentTime = System.currentTimeMillis()
//        "currentTime = $currentTime".e()
        if (currentTime >= entTime) {
            throw IllegalArgumentException("xxxxxx")
        }
        mBinding?.splashIv?.loadImage(
            data = R.drawable.bg_logo,
            width = screenWidth,
            height = screenHeight,
        )
    }

    override fun initData() {
        Looper.getMainLooper()?.apply {
            Handler(this).postDelayed({
                launchSelf()
            }, 1000)
        }

    }

    override fun startObserve() {
    }

    override fun onBackPressed() {
        super.onBackPressed()
        dialog?.dismiss()
        KtxActivityManager.exitApp()
    }


    private fun launchSelf() {
        handFlashAD()
    }

    override fun onDestroy() {
        super.onDestroy()
        dialog?.dismiss()
    }
}